package com.syp.hehe.web.controller;

import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.syp.hehe.domain.vo.ThumbVo;
import com.syp.hehe.service.ThumbServiceI;
import com.syp.hehe.util.Constants;
import com.syp.hehe.util.ProcessResult;
import com.syp.hehe.util.SessionInfo;
import com.syp.hehe.util.paging.PageOffSet;

@Controller
@RequestMapping({ "/thumb" })
public class ThumbController {
	
	private static final Logger logger = LoggerFactory.getLogger(ThumbController.class);
	
	@Autowired
	private ThumbServiceI thumbService;
	
	@RequestMapping(value = "/up/{articleId}")
	@ResponseBody
	public ProcessResult up(@PathVariable(value = "articleId") Long articleId) {
		ProcessResult pr = new ProcessResult();
		try {
			ThumbVo thumb = new ThumbVo();
			thumb.setArticleId(articleId);
			thumb.setUuid(UUID.randomUUID().toString());
			
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
			SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
			thumb.setUsername(sessionInfo.getUsername());			
			this.thumbService.up(thumb);
			
			pr.setSuccess(true);
			pr.setMessage("给赞成功。");
		} catch (Exception e) {
			logger.error("给赞失败。", e);

			pr.setSuccess(false);
			pr.setMessage("给赞失败。");
		}
		return pr;
	}
	
	@RequestMapping(value = "/unup/{articleId}")
	@ResponseBody
	public ProcessResult unup(@PathVariable(value = "articleId") Long articleId) {
		ProcessResult pr = new ProcessResult();
		try {			
			HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
			SessionInfo sessionInfo = (SessionInfo) session.getAttribute("sessionInfo");
			
			this.thumbService.unup(sessionInfo.getUsername(), articleId);
			
			pr.setSuccess(true);
			pr.setMessage("取消赞成功。");
		} catch (Exception e) {
			logger.error("取消赞失败。", e);

			pr.setSuccess(false);
			pr.setMessage("取消赞失败。");
		}
		return pr;
	}
	
	@RequestMapping("/list/after/{referId}")
	@ResponseBody
	public ProcessResult list(Long articleId, @PathVariable(value = "referId") Long referId) {	
		ProcessResult pr = new ProcessResult();
		try {
			PageOffSet<ThumbVo> pageOffSet = this.thumbService.getPageOffSetList(articleId, referId, Constants.COMMENT_PIGE_SIZE);

			pr.setSuccess(true);
			pr.setMessage("查询评论列表成功。");
			pr.getRet().put("page", pageOffSet);
		} catch (Exception e) {
			logger.error("查询评论列表失败。", e);

			pr.setSuccess(false);
			pr.setMessage("查询评论列表失败。");
		}
		return pr;
	}
	
	
	
}
